﻿using Liang.DataBase.BLL;
using Liang.DataBase.DAL;
using Liang.DataBase.Model;
using Liang.DataBase.Model.Entity;
using Liang.DataBase.Tools;
using Liang.Model;
using Liang.UI;
using Liang.UI.Tools;
using System;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Client
{
    public partial class FrmOrgan : DockContent
    {
        OrganBLL organBLL = new OrganBLL();

        public FrmOrgan()
        {
            InitializeComponent();  
        }

        private void FrmOrgan_Load(object sender, EventArgs e)
        {
            initView();
            //this.btnAdd.Visible = UserHelper.admin() ? true : false;
        }

        /// <summary>
        /// 初始化dataGridView
        /// </summary>
        public void initView()
        {
            dataView.AutoGenerateColumns = false;
            dataView.Columns.Add(FormHelper.createTextColumn("organId", "organId", 100, false));
            dataView.Columns.Add(FormHelper.createTextColumn("名称", "name", 200, true));
            dataView.Columns.Add(FormHelper.createTextColumn("地址", "address", 200, true));
            dataView.Columns.Add(FormHelper.createTextColumn("机构类型", "basicdata.des", 200, true));
            dataView.Columns.Add(FormHelper.createTextColumn("记录日期", "createTime", 100, true));
            //追加
            dataView.Columns.Insert(dataView.Columns.Count, FormHelper.createLinkColumn("Detail", "编辑",true));
            this.pager.Bind();//绑定  
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private int queryBind()
        {
            // 分页信息
            PageInfo pagerInfo = new PageInfo()
            {
                CurrentPageIndex = pager.PageCurrent,
                PageSize = pager.PageSize,
                RecordCount = pager.PageSize * pager.PageCurrent
            };

            // 查询条件
            var where = LambdaHelper.True<sys_organ>();

            //if (!UserHelper.admin())
            //    where = where.And(p => p.organId == UserHelper.users.person.organId);

            if (!string.IsNullOrEmpty(this.extOrganName.Text.Trim()))
            {
                where = where.And(p => this.extOrganName.Text.Trim().Contains(p.name));
            }

            var list = organBLL.FindWithPagerAndOrderby(where, ref pagerInfo, "organId");
            //绑定分页控件   
            pager.bindingSource.DataSource = list;
            pager.bindingNavigator.BindingSource = pager.bindingSource;
            //分页控件绑定DataGridView   
            dataView.DataSource = pager.bindingSource;
            //返回总记录数   
            return pagerInfo.RecordCount;
        }

        /// <summary>
        /// 显示编辑页面
        /// </summary>
        public void showEdit(sys_organ organ = null)
        {
            FrmOrganEdit organEdit = new FrmOrganEdit(organ);

            if (DialogResult.OK == organEdit.ShowDialog())
            {
                pager.Bind();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            pager.Bind();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            showEdit();
        }

        private void dataView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataView.Columns[e.ColumnIndex].Name == "Detail" && 0 <= e.RowIndex)
            {
                Guid organId = (Guid)dataView.Rows[e.RowIndex].Cells["organId"].Value;
                sys_organ organ = organBLL.FindByID(organId);

                if (null == organ)
                {
                    MessageBox.Show("未知错误");
                }
                else
                {
                    showEdit(organ);
                }
            }           
        }

        private int pager_EventPaging()
        {
            return queryBind();
        }

        private EventPagingArg pager_EventDelItem()
        {
            return new EventPagingArg("删除机构", dataView);
        }

        private EventPagingArg pager_EventExcel()
        {
            return new EventPagingArg("机构管理", this.dataView);
        }
    }
}
